﻿using ShareHelper;
using System;
using System.Windows.Forms;

namespace 小说爬取
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Log Log;

        private void Form1_Load(object sender, EventArgs e)
        {
            Log = new Log(textBox_Log);
            SetEnadled(1);
            comboBox1.SelectedIndex = 2;
        }

        public static IBeginParse Chapter;

        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="sender"> 参数</param>
        /// <param name="e"></param>
        private void Button_Parse_Click(object sender, EventArgs e)
        {
            if (Parse(textBox_Url.Text, out var chapter))
            {
                SetEnadled(0);
                Chapter = chapter;
                Chapter.Message += Chapter_Message;
                Chapter.BeginParse();
            }
            else
                Log.LabelText("未知小说平台");
        }

        private void Button_Download_Click(object sender, EventArgs e)
        {
            SetEnadled(0);
            Chapter.BeginDownload(int.Parse(comboBox1.SelectedItem.ToString()));
        }

        private void Button_Write_Click(object sender, EventArgs e)
        {
            using (var dat = new SaveFileDialog())
            {
                dat.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                dat.Filter = "txt files (*.txt)|*.txt";
                dat.FileName = Chapter.Name;
                dat.FilterIndex = 2;
                dat.RestoreDirectory = false;
                if (dat.ShowDialog() == DialogResult.OK)
                {
                    SetEnadled(0);
                    Chapter.BeginWrite(dat.FileName);
                }
            }
        }

        private void SetEnadled(int a)
        {
            Log.BeginInvoke(new Action(() =>
           {
               button_Parse.Enabled = a > 0;
               button_Download.Enabled = a > 1;
               button_Write.Enabled = a > 2;
           }));
        }

        private void Chapter_Message(object sender, MessageEventArgs e)
        {
            Log.LabelText(e.Message);
            if (sender is ChapterParse chapter)
            {
                switch (e.Type)
                {
                    case MessageType.Parse:
                        OnParse(chapter, e);
                        break;

                    case MessageType.Chapter:
                        Log.LabelText($"共:{chapter.Count}.正在下载:{chapter.UnderwayCount}.剩余:{chapter.BeforeCount}.已下载:{chapter.PerformCount}.");
                        break;

                    case MessageType.Download:
                        OnDownload(chapter, e);
                        break;

                    case MessageType.Write:
                        OnWrite(chapter, e);
                        break;

                    case MessageType.Unknown:

                        break;

                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 小说列表解析完毕回调
        /// </summary>
        private void OnParse(ChapterParse chapter, MessageEventArgs e)
        {
            if (e.State)
            {
                Log.LabelText($"共:{chapter.Count},章节");
                SetEnadled(2);
            }
            else
                SetEnadled(1);
        }

        private void OnDownload(ChapterParse chapter, MessageEventArgs e)
        {
            if (e.State)
            {
                SetEnadled(3);
            }
            else
                SetEnadled(2);
        }

        private void OnWrite(ChapterParse chapter, MessageEventArgs e)
        {
            SetEnadled(3);
        }

        public static bool Parse(string url, out ChapterParse chapter)
        {
            chapter = null;
            if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
            {
                switch (uri.Host)
                {
                    case "www.biqige.com":
                        chapter = new Biqige(uri);
                        break;

                    case "www.37zw.net":
                        chapter = new Zw37(uri);
                        break;

                    case "www.nitianxieshen.com":
                        chapter = new Citianxieshen(uri);
                        break;

                    case "www.biquge.biz":
                        chapter = new Biquge(uri);
                        break;

                    default:
                        break;
                }
            }
            return chapter != null;
        }
    }
}